package edu.zzuli.assistant.api.util;

import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class NormalStrategy implements ExclusionStrategy {
	private static Logger log = LoggerFactory.getLogger(NormalStrategy.class);
	private Class<?> target;   //目标转换 class
	private String[] fields;   //保留的字段
	private Class<?> clazz;  //保留的内部类  class
	private String[]   clazzFields;   //保留的内部类 需要的保留的字段

	private boolean reverse;   //是保留还是跳过 true 表示保留

	public NormalStrategy(Class<?> target) {
		super();
		this.target = target;
	}

	@Override
	public boolean shouldSkipClass(Class<?> class1) {
		return false;
	}

	@Override
	public boolean shouldSkipField(FieldAttributes fieldattributes) {
		Class<?> owner = fieldattributes.getDeclaringClass();
		Class<?> c = fieldattributes.getDeclaredClass();
		String f = fieldattributes.getName();
		boolean isSkip = false;
		//目标class
		if (owner == target) {
			if (ArrayUtils.contains(fields, f)) {
				log.debug("fitler field:{} for class:{}", f, owner);
				isSkip = true;
			}
		}
		//特殊处理class
		else if(owner == clazz){
			if(ArrayUtils.contains(clazzFields, f)){
				isSkip = true;
			}
		}
		if (reverse) {
			isSkip = !isSkip;
		}
		return isSkip;
	}

	public void setFields(String[] fields) {
		this.fields = fields;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}
	public String[] getClazzFields() {
		return clazzFields;
	}

	public void setClazzFields(String[] clazzFields) {
		this.clazzFields = clazzFields;
	}
	public void setReverse(boolean reverse) {
		this.reverse = reverse;
	}
}


